﻿using MesBSW.BLL.Mes;
using MesBSW.DAL.MesDB;
using MesBSW.DAL.MesDTO;
using MesBSW.Tools;
using MesBSW.View.Dialog.Mes;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MesBSW.View.Mes
{
    public partial class SiteGroupFrm : UIPage
    {
        #region "内部变量"
        private SiteControler _ctrl = new SiteControler();                          //业务逻辑层
        private List<SiteGroupDTO> _group_list = new List<SiteGroupDTO>();
        private SiteGroupDTO _curr_group = new SiteGroupDTO();
        private SqlSugarClient Db = SqlSugares.GetInstance();
        #endregion

        public SiteGroupFrm()
        {
            InitializeComponent();
        }

        #region "控件事件"
        /// <summary>
        /// 新增站点组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddStand_Click(object sender, EventArgs e)
        {
            SiteGroup groupBean = new SiteGroup();
            groupBean.ID = 0;
            groupBean.IsDel = 0;
            AddSiteGroupDia tmpDia = new AddSiteGroupDia(groupBean);
            tmpDia.ShowDialog();
            this.AfterSaveDate();
        }

        /// <summary>
        /// 查询所有站点组别信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            DataSelect
                ();
        }

        /// <summary>
        /// 当用户在界面左边选择组别时，将对应的明细信息显示在右边列表中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lvwSiteGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwSiteGroup.Items.Count > 0 && this.lvwSiteGroup.SelectedItems.Count > 0 && this._group_list != null && this._group_list.Count > 0)
            {
                string tmpCode = lvwSiteGroup.SelectedItems[0].SubItems[0].Text;
                var query = this._group_list.Where(g => g.GroupCode == tmpCode);
                if (query != null && query.Count() > 0)
                {
                    this._curr_group = query.First();
                    this.InitSiteGroupDataGridView(this._curr_group);
                }
            }

        }

        /// <summary>
        /// 清空界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.ClearDate();
        }
        #endregion

        #region "内部方法"
        /// <summary>
        /// 清空界面资料
        /// </summary>
        private void ClearDate()
        {
            this._group_list = new List<SiteGroupDTO>();
            this._curr_group = null;
            this.lvwSiteGroup.Items.Clear();
            this.InitSiteGroupDataGridView(null);
        }

        /// <summary>
        /// 保存之后，重查所有数据
        /// </summary>
        private void AfterSaveDate()
        {
            DataSelect();
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        private void DataSelect()
        {
            this._group_list = this._ctrl.SearchAllSiteGroup();
            this.InitListViewForSiteGroup(this._group_list);
        }

        /// <summary>
        /// 初始化界面左边的站点组列表
        /// </summary>
        /// <param name="groups"></param>
        private void InitListViewForSiteGroup(List<SiteGroupDTO> groups)
        {
            if (this.lvwSiteGroup.Items.Count > 0)
            {
                this.lvwSiteGroup.Items.Clear();
            }

            foreach (SiteGroupDTO group in groups)
            {
                ListViewItem item = new ListViewItem(new string[] { group.GroupCode, group.GroupName, group.IsDel == 1 ? "是" : "否" });
                this.lvwSiteGroup.Items.Add(item);
            }

            if (this.lvwSiteGroup.Items.Count > 0)
            {
                this.lvwSiteGroup.Items[0].Selected = true;
            }
        }

        /// <summary>
        /// 根据所选的站点组别，查询组别中的信息
        /// </summary>
        /// <param name="groupBean"></param>
        private void InitSiteGroupDataGridView(SiteGroupDTO groupBean)
        {
            DataTable tmpDt = new DataTable();
            tmpDt.Columns.Add("id");
            tmpDt.Columns.Add("site_id");
            tmpDt.Columns.Add("site_code");
            tmpDt.Columns.Add("site_name");
            tmpDt.Columns.Add("site_sn");
            tmpDt.Columns.Add("parent_id");
            tmpDt.Columns.Add("is_del");
            tmpDt.Columns.Add("is_del_name");
            tmpDt.Columns.Add("group_id");
            tmpDt.Columns.Add("sort");
            tmpDt.Columns.Add("is_last");
            tmpDt.Columns.Add("is_last_name");

            if (groupBean != null && groupBean.SiteInfoList != null && groupBean.SiteInfoList.Count > 0)
            {
                foreach (var infoBean in groupBean.SiteInfoList.OrderBy(s => s.Sort).ToList())
                {
                    DataRow dr = tmpDt.NewRow();
                    dr["id"] = infoBean.ID;
                    dr["site_id"] = infoBean.SiteID;
                    dr["site_code"] = infoBean.SiteInfo.SiteCode;
                    dr["site_name"] = infoBean.SiteInfo.SiteName;
                    dr["site_sn"] = infoBean.SiteInfo.SiteShortName;
                    dr["parent_id"] = infoBean.SiteInfo.ParentID;
                    dr["is_del"] = infoBean.SiteInfo.IsDel;
                    dr["is_del_name"] = false;
                    if (infoBean.SiteInfo.IsDel == 1)
                    {
                        dr["is_last_name"] = true;
                    }
                    dr["group_id"] = infoBean.GroupID;
                    dr["sort"] = infoBean.Sort;
                    dr["is_last"] = infoBean.IsLast;
                    dr["is_last_name"] = false;
                    if (infoBean.IsLast == 1)
                    {
                        dr["is_last_name"] = true;
                    }
                    tmpDt.Rows.Add(dr);
                }
            }
            this.dgvSiteInfo.DataSource = tmpDt;
        }

        #endregion

        /// <summary>
        /// 调整站点设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this._curr_group != null)
            {
                SiteInfoGroupSettingFrm tmpDia = new SiteInfoGroupSettingFrm(this._curr_group);
                tmpDia.ShowDialog();

                this.AfterSaveDate();
            }
            else
            {
                UIMessageBox.ShowWarning("请在界面左边的站点组别列表中选择后再点击【调整站点】！");
            }
        }
    }
}
